package com.yunpai.thread;

/**
 * 模拟次窗口卖票案列  继承Thread的方式 与代码块方式
 */
public class WindThreadTicketTypeOne extends Thread {
    static int ticket = 100;
    //代码块锁
    static Object o = new Object();

    @Override
    public void run() {
        while (true) {
            synchronized (o) {//同步监视器为当前的静态对象
                if (ticket > 0)
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                System.out.println(Thread.currentThread().getName() + "卖的是第" + ticket + "张");
                ticket--;
            }
        }

    }


    public static void main(String[] args) {
        WindThreadTicketTypeOne windows1 = new WindThreadTicketTypeOne();
        WindThreadTicketTypeOne windows2 = new WindThreadTicketTypeOne();
        WindThreadTicketTypeOne windows3 = new WindThreadTicketTypeOne();
        windows1.setName("窗口一");
        windows2.setName("窗口二");
        windows3.setName("窗口三");
        windows1.start();
        windows2.start();
        windows3.start();
    }
}
